package com.eascs.crm.nsEditDetail.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.Date;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.nsEditDetail.entity.HouseInfoEntity;
import com.eascs.crm.nsEditDetail.entity.HouseInfoLogEntity;
import com.eascs.crm.nsEditDetail.form.HouseInfoForm;
import com.eascs.crm.nsEditDetail.form.HouseLogInfoForm;
import com.eascs.crm.nsEditDetail.service.HouseInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.UserHolder;

/**
 * 夫妻双方房产信息
 * 
 * @author lwx
 *
 */
@Controller
@RequestMapping("/house")
public class HouseInfoRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(HouseInfoRestController.class);

	@Value("${web.crm.url}")
	private String crmUrl;
	
	@Inject
	HouseInfoService houseInfoService;
	@Autowired
	O2oLoanapplyService o2oLoanapplyService;

	@RequestMapping("/house_list")
	public PaginationRestResult house_list(String loanapplyId,@RequestParam(defaultValue="0") Integer showPage, String type, String approvalStatus) {
		logger.info("房产列表loanapplyId：{}", loanapplyId);
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyId);
		PageResult<Object[]> pageResult = houseInfoService.findHouseListInfo(loanapplyId,showPage,type,approvalStatus);
		String[] oatitles = { "房产地址", "房产性质", "是否按揭", "估值方法", "信贷经理估值"};
		String o2ourl = "/house/house_list";
		if (StringUtil.isNotBlank(loanapplyId)) {
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanapplyId", loanapplyId);
			o2ourl = o2ourl + "&showPage=";
		} else {
			o2ourl = o2ourl + "?showPage=";
		}
		
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, oatitles, o2ourl);
		Button addressButton = null;
		Button editButton = null;
		Button delButton = null;

		if(StringUtil.isNotBlank(approvalStatus)){
//			if (!"UNDO".equals(approvalStatus)) {
				addressButton = ButtonHelper.createAjaxButton(null, "/house/view_address?houseId=", "UpdateInternalInfo");
//			}
		}else {
			if (!"detail".equals(type)) {
				addressButton = ButtonHelper.createAjaxButton(null, "/house/view_address?houseId=", "UpdateInternalInfo");
				if(StatusContexts.O2O_ZX_END.equals(o2oLoanapplyEntity.getWfType())){
					editButton = ButtonHelper.createAjaxButton("修改", "/house/house_op?houseId=", "UpdateInternalInfo");
					delButton = ButtonHelper.createAjaxButton("删除 ", "/house/house_del?houseId=", "UpdateInternalInfo");
				}
			}
		}
		Button[] detailButton = new Button[] { editButton, delButton };
		Button[] titleButtons = new Button[] { addressButton };
		SelectDataOption[] selectDataOptions = {null,new SelectDataOption("CQXZ"),new SelectDataOption("SFAJ"),new SelectDataOption("GZFF"), null,null};
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setRendTo("#o2o_house");
		paginationInfo.setFormats(new Format[] { null, null, null, null, new DecimalFormat(",##0.00"), null });
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	// 新增修改
	@RequestMapping(value = "/house_op")
	public HtmlRestResult houseOp(String loanapplyId,String houseId) {
		HouseInfoEntity vo = new HouseInfoEntity();
		if (StringUtil.isNotBlank(houseId)) {
			vo = houseInfoService.findHouseInfoEntity(houseId);
		}
		ViewContext.put("loanapplyId", loanapplyId);
		ViewContext.put("vo", vo);
		ViewContext.put("crmUrl", crmUrl);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/house/house_op");
	}

	@RequestMapping(value = "/house_del")
	public BaseRestResult<String> houseDel(String houseId) {
		try {
			houseInfoService.houseDel(houseId);
		} catch (Exception e) {
			return BaseRestResult.warn("数据库操作异常");
		}
		return BaseRestResult.info("操作成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_house");
	}

	@RequestMapping("/house_save")
	public BaseRestResult<String> houseSave(HouseInfoForm form) {
		if (form != null && StringUtil.isNotBlank(form.getHouseId())) {// 修改
			HouseInfoEntity house = houseInfoService.findHouseInfoEntity(form.getHouseId());
			try {
				houseInfoService.houseUpdate(house, form);
			} catch (Exception e) {
				return BaseRestResult.warn("数据库操作异常");
			}
		} else {// 新增
			HouseInfoEntity house = new HouseInfoEntity();
			try {
				houseInfoService.houseSave(house, form);
			} catch (Exception e) {
				return BaseRestResult.warn("数据库操作异常");
			}
		}
		return BaseRestResult.info("成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_house");
	}

	@RequestMapping("/house_list_form")
	public PaginationRestResult house_list_form(String loanapplyId,@RequestParam(defaultValue="0") Integer showPage) {
		PageResult<Object[]> pageResult = houseInfoService.findHouseListInfo(loanapplyId,showPage,null);
		String[] oatitles = { "房产地址", "房产性质", "是否按揭", "估值方法", "信贷经理估值", "估值复核（万元）" };
		String o2ourl = "/house/house_list_form";
		if (StringUtil.isNotBlank(loanapplyId)) {
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanapplyId", loanapplyId);
			o2ourl = o2ourl + "&showPage=";
		} else {
			o2ourl = o2ourl + "?showPage=";
		}
		Button addressButton = ButtonHelper.createAjaxButton("查看", "/house/view_address?houseId=", "UpdateInternalInfo");
		Button[] titleButtons = new Button[] { addressButton };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, oatitles, o2ourl);
		AjaxButton editButton = new AjaxButton("修改", "/house/ns_house_view?houseId=");
		Button[] detailButton = new Button[] { editButton };
		SelectDataOption[] selectDataOptions = {null,new SelectDataOption("CQXZ"),new SelectDataOption("SFAJ"),new SelectDataOption("GZFF"), null,null};
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setRendTo("#o2o_house_form");
		paginationInfo.setFormats(new Format[] { null, null, null, null, null, null });
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}
	
	@RequestMapping(value = "/ns_house_view")
	public HtmlRestResult nsHouseView(String houseId) {
		HouseInfoEntity vo = new HouseInfoEntity();
		HouseInfoLogEntity log =new HouseInfoLogEntity();
		if (StringUtil.isNotBlank(houseId)) {
			vo = houseInfoService.findHouseInfoEntity(houseId);
			log = houseInfoService.findHouseLogEntity(houseId);
		}
		ViewContext.put("vo", vo);
		ViewContext.put("log", log);
		ViewContext.put("crmUrl", crmUrl);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/house/house_op_form");
	}
	
	@RequestMapping("/house_form_save")
	public BaseRestResult<String> houseFormSave(HouseLogInfoForm form) {
		HouseInfoLogEntity log = new HouseInfoLogEntity();
		log.setHouseInfoId(form.getHouseId());
		log.setOpPsn(UserHolder.getUser().getCname());
		log.setOpNo(UserHolder.getUser().getCode());
		log.setOpDate(new Date());
		log.setValuation(form.getValuation());
		log.setHousePrice(form.getHousePriceReview());
		int i = houseInfoService.insert(log);
		if(i>0){
			return BaseRestResult.info("成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_house_form");
		}else{
			return BaseRestResult.warn("操作失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_house_form");
		}
	}
	
	/**
	 *  CXW 贷款详情显示证件号码等信息星号 http://tick.o2ojr.eascs.com/ticket/view/loan-12592
	 * @param houseId
	 * @return
	 */
	@RequestMapping("/view_address")
	public BaseRestResult<String> view_address(String houseId) {
		HouseInfoEntity houseInfoEntity = houseInfoService.findHouseInfoEntity(houseId);
		ViewContext.put("vo", houseInfoEntity);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/house/view_address");
	}
}
